package zhishuang.wang.sansi.tcp.client;

import zhishuang.wang.sansi.fcms.protocol.FcmsProtocolDeal;
import zhishuang.wang.sansi.fcms.protocol.ProtocolPackage;
import zhishuang.wang.sansi.tools.PrintRunLog;
import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.time.DateFormat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 10:49
 * @Email 1406110602@qq.com
 */
public class TcpSocketClient {

    private Socket clientSocket = null;
    private SocketAddress mSocketAddress;
    private OutputStream mOutputStream = null;
    private InputStream mInputStream = null;
    private int maxDeviceDataRevSize = 4096;
    private int readTimeOut = 5000;

    public TcpSocketClient(String ipAddr, int ipPort, int timeOut) {
        try {
            this.clientSocket = new Socket();
            this.clientSocket.setKeepAlive(true);
            this.mSocketAddress = new InetSocketAddress(ipAddr, ipPort);
            this.clientSocket.connect(this.mSocketAddress, timeOut);
            this.clientSocket.setSoTimeout(this.readTimeOut);
            this.mOutputStream = this.clientSocket.getOutputStream();
            this.mInputStream = this.clientSocket.getInputStream();
        } catch (IOException var5) {
            var5.printStackTrace();
            this.tcpSocketClose();
        }

    }

    public int tcpSocketClose() {
        try {
            if (this.clientSocket != null) {
                this.clientSocket.close();
            }

            if (this.mOutputStream != null) {
                this.mOutputStream.close();
            }

            if (this.mInputStream != null) {
                this.mInputStream.close();
            }
        } catch (IOException var5) {
            var5.printStackTrace();
        } finally {
            this.clientSocket = null;
            this.mOutputStream = null;
            this.mInputStream = null;
            return 0;
        }
    }

    public byte[] fileDataPassThrough(byte[] data, int blockSize) {
        try {
            if (this.mOutputStream != null && this.mInputStream != null) {
                blockSize *= 2;
                byte[] revDeviceDataBuff = new byte[blockSize];
                byte[] buffer = new byte[blockSize];
                int revDeviceDataLen = 0;
                this.mOutputStream.write(data);

                while(true) {
                    int size = this.mInputStream.read(buffer);
                    if (size <= 0) {
                        break;
                    }

                    System.arraycopy(buffer, 0, revDeviceDataBuff, revDeviceDataLen, size);
                    revDeviceDataLen += size;
                    byte[] validData = FcmsProtocolDeal.fcmsValidData(revDeviceDataBuff, revDeviceDataLen);
                    if (validData != null) {
                        return ProtocolPackage.revToBaseData(validData);
                    }
                }
            }
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return null;
    }

    public ReturnData sendDataToService(byte[] data, int deviceType, int cmdType) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);

        try {
            if (this.mOutputStream != null && this.mInputStream != null) {
                byte[] revDeviceDataBuff = new byte[this.maxDeviceDataRevSize];
                byte[] buffer = new byte[this.maxDeviceDataRevSize];
                int revDeviceDataLen = 0;
                
                PrintRunLog.printByteArrayInfor("fcms send:", data);
                this.mOutputStream.write(data);

                label44:
                while(true) {
                    int size = this.mInputStream.read(buffer);
                    if (size <= 0) {
                        break;
                    }

                    System.arraycopy(buffer, 0, revDeviceDataBuff, revDeviceDataLen, size);
                    revDeviceDataLen += size;
                    byte[] validData = FcmsProtocolDeal.fcmsValidData(revDeviceDataBuff, revDeviceDataLen);
                    if (validData != null) {
                        byte[] baseData = ProtocolPackage.revToBaseData(validData);
                        PrintRunLog.printByteArrayInfor("fcms rev:", baseData);
                        ReturnData dealRd = null;
                        switch (deviceType) {
                            case 1:
                                switch (cmdType) {
                                    case 1:
                                        dealRd = FcmsProtocolDeal.fcmsFalutDeal(baseData, baseData.length);
                                        break;
                                    case 2:
                                        dealRd = FcmsProtocolDeal.fcmsShowInforDeal(baseData, baseData.length);
                                        break;
                                    case 3:
                                    case 4:
                                    case 5:
                                    case 8:
                                    case 9:
                                    case 17:
                                    case 18:
                                    case 19:
                                    case 20:
                                    case 21:
                                        dealRd = FcmsProtocolDeal.fcmsCommResDeal(baseData, baseData.length);
                                        break;
                                    case 6:
                                        dealRd = FcmsProtocolDeal.fcmsLightAndModeReadDeal(baseData, baseData.length);
                                        break;
                                    case 7:
                                        dealRd = FcmsProtocolDeal.fcmsParamReadDeal(baseData, baseData.length);
                                        break;
                                    case 10:
                                        dealRd = FcmsProtocolDeal.fcmsTimeReadDeal(baseData, baseData.length);
                                    case 11:
                                    case 12:
                                    case 13:
                                    case 14:
                                    case 15:
                                    case 16:
                                    case 22:
                                    default:
                                        break;
                                    case 23:
                                        dealRd = FcmsProtocolDeal.fcmsDevVerionDeal(baseData, baseData.length);
                                }
                            default:
                                if (dealRd != null) {
                                    return dealRd;
                                }
                                break label44;
                        }
                    }
                }

                if (rd.getCode() != 0) {
                    rd.setMessage("设备应答数据错误，请检查后重试");
                }
            } else {
                rd.setMessage("未连接到设备，请首先与设备创建连接");
            }
        } catch (IOException var12) {
            var12.printStackTrace();
            rd.setMessage("设备信息返回超时，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

}
